package com.lzy.huijinjiazu.mvp.ui.fragment;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.flyco.roundview.RoundTextView;
import com.gyf.barlibrary.ImmersionBar;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.LogUtils;
import com.lzy.huijinjiazu.R;
import com.lzy.huijinjiazu.di.component.DaggerHomeComponent;
import com.lzy.huijinjiazu.di.module.HomeModule;
import com.lzy.huijinjiazu.mvp.contract.HomeContract;
import com.lzy.huijinjiazu.mvp.model.entity.Home;
import com.lzy.huijinjiazu.mvp.presenter.HomePresenter;
import com.lzy.huijinjiazu.mvp.ui.adapter.GoodsAdapter;
import com.lzy.huijinjiazu.mvp.ui.adapter.HomeCardAdapter;
import com.lzy.huijinjiazu.mvp.ui.adapter.TourismAdapter;
import com.lzy.huijinjiazu.mvp.ui.widget.BannerGridLoader;
import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

//首页
public class HomeFragment extends BaseFragment<HomePresenter> implements HomeContract.View {
    //所有banner都使用fit_center防止内存溢出
    @BindView(R.id.banner)
    Banner mBanner;
    @BindView(R.id.vf_ad)
    ViewFlipper mVfAd;
    @BindView(R.id.rv_luxury)
    RecyclerView mRvLuxury;
    @BindView(R.id.rv_recommend)
    RecyclerView mRvRecommend;
    @BindView(R.id.rv_card)
    RecyclerView mRvCard;
    @BindView(R.id.ll_tab_content)
    LinearLayout mLlTabContent;
    @BindView(R.id.tv_content_love1)
    TextView mTvContentLove1;
    private GoodsAdapter mLuxuryAdapter;
    private TourismAdapter mTourismAdapter;
    private HomeCardAdapter mHomeCardAdapter;

    public static HomeFragment newInstance() {
        HomeFragment fragment = new HomeFragment();
        return fragment;
    }

    @Override
    public void setupFragmentComponent(AppComponent appComponent) {
        DaggerHomeComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .homeModule(new HomeModule(this))
                .build()
                .inject(this);
    }

    @Override
    public View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mTvContentLove1.setText(Html.fromHtml(ArmsUtils.getString(getContext(), R.string.symbol_moeny, "47500")));
//        mPresenter.getInex();
        initRecyclerView();
        initBannerData();
        initRvCard();
        initAdData();
        initRvLuxury();
        initRvTourism();
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.setFitsSystemWindows(getActivity());
    }

    private void tvTabChange(int position) {
        for (int i = 0; i < mLlTabContent.getChildCount(); i++) {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.rightMargin = ArmsUtils.dip2px(getContext(), 4);
            RoundTextView tvView = (RoundTextView) mLlTabContent.getChildAt(i);
            if (i == position) {
                tvView.getDelegate().setBackgroundColor(ContextCompat.getColor(getContext(), R.color.colorAccent));
                params.width = ArmsUtils.dip2px(getContext(), 13);
            } else {
                tvView.getDelegate().setBackgroundColor(0xffC3C3C3);
                params.width = ArmsUtils.dip2px(getContext(), 9);
            }
            params.height = ArmsUtils.dip2px(getContext(), 4);
            tvView.setLayoutParams(params);
        }


    }

    private void initRecyclerView() {

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mRvCard.setLayoutManager(linearLayoutManager);
        mHomeCardAdapter = new HomeCardAdapter();
        mRvCard.setAdapter(mHomeCardAdapter);
        mRvCard.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
                // 当不滚动时
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    //获取最后一个完全显示的ItemPosition
                    int lastVisibleItem = manager.findLastCompletelyVisibleItemPosition();

                    int totalItemCount = manager.getItemCount();
                    if (lastVisibleItem != -1) {
                        tvTabChange(lastVisibleItem);
                    }
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });

        ArmsUtils.configRecyclerView(mRvRecommend, new LinearLayoutManager(getContext()) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        mTourismAdapter = new TourismAdapter();
        mRvRecommend.setAdapter(mTourismAdapter);

        ArmsUtils.configRecyclerView(mRvLuxury, new GridLayoutManager(getContext(), 2) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        mRvLuxury.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
                super.getItemOffsets(outRect, itemPosition, parent);
                if (itemPosition == 0) {
                    outRect.right = ArmsUtils.dip2px(getContext(), 2.5f);
                }
                if (itemPosition == 1) {
                    outRect.left = ArmsUtils.dip2px(getContext(), 2.5f);
                }
            }
        });
        mLuxuryAdapter = new GoodsAdapter();
        mRvLuxury.setAdapter(mLuxuryAdapter);
    }


    //banner初始化数据
    private void initBannerData() {
        List<Integer> imgs = new ArrayList<>();
        imgs.add(R.mipmap.bg_test_10);
        imgs.add(R.mipmap.bg_test_10);
        imgs.add(R.mipmap.bg_test_10);
        mBanner.setImages(imgs)
                .setImageLoader(new BannerGridLoader())
                .setOnBannerListener(index -> {

                })
                .start();
    }

    //广告数据
    private void initAdData() {
        for (int i = 0; i < 3; i++) {
            View view = LayoutInflater.from(getContext()).inflate(R.layout.view_ad, null, false);
            TextView tvAd = (TextView) view;
            tvAd.setText("内部消息，加盟商可见" + i);
            tvAd.setOnClickListener(view1 -> {
                ArmsUtils.makeText(getContext(), tvAd.getText().toString());
            });
            mVfAd.addView(view);
        }
    }

    //限时抢购card
    private void initRvCard() {
        List<String> data = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            data.add(new String());
        }
        mHomeCardAdapter.setNewData(data);
        addTabView(data.size());
    }

    //奢饰品数据
    private void initRvLuxury() {
        List<String> data = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            data.add(new String());
        }
        mLuxuryAdapter.setNewData(data);
    }

    private void addTabView(int size) {
        for (int i = 0; i < size; i++) {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.rightMargin = ArmsUtils.dip2px(getContext(), 4);
            View view = LayoutInflater.from(getContext()).inflate(R.layout.item_home_tab_textview, mLlTabContent, false);
            RoundTextView tvView = (RoundTextView) view;
            if (i == 0) {
                tvView.getDelegate().setBackgroundColor(ContextCompat.getColor(getContext(), R.color.colorAccent));
                params.width = ArmsUtils.dip2px(getContext(), 13);
            } else {
                tvView.getDelegate().setBackgroundColor(0xffC3C3C3);
                params.width = ArmsUtils.dip2px(getContext(), 9);
            }
            params.height = ArmsUtils.dip2px(getContext(), 4);
            tvView.setLayoutParams(params);
            mLlTabContent.addView(tvView);
        }
    }

    //旅游数据
    private void initRvTourism() {
        List<String> data = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            data.add(new String());
        }
        mTourismAdapter.setNewData(data);
    }

    @Override
    public void setData(Home home) {
    }
}
